package com.yyfax.pay.common.model.to;

import org.apache.commons.lang3.StringUtils;

import com.yyfax.framework.exception.YYException;
import com.yyfax.framework.model.Params;
import com.yyfax.framework.yyfax.YYFaxSignUtil;
import com.yyfax.framework.yyfax.enums.RspCodeEnum;
import com.yyfax.pay.common.exception.YYPayException;

/**
 * yypay请求参数超类
 * 
 * @author buyi
 * @date 2017-04-14 17:06:55
 * @since v1.0.0
 */
@SuppressWarnings("serial")
public abstract class YYPayParams extends Params {
	/** 签名串 */
	private String sign;

	public abstract String[] signFields();

	public String sign(String key) {
		return YYFaxSignUtil.md5Sign(this, key, signFields());
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	/**
	 * 验证签名
	 * 
	 * @author buyi
	 * @date 2017-05-03 15:49:03
	 * @since v1.0.0
	 * @param key
	 * @return
	 * @throws YYException
	 */
	public boolean checkSign(String key) throws YYException {
		if (StringUtils.isBlank(this.sign)) {
			throw new YYPayException(RspCodeEnum.COMMON_ERROR_NO_SIGN_AUTHENTICATION, "签名失败", "无效请求");
		}

		String srcSign = sign(key);
		if (!StringUtils.equals(srcSign, this.sign)) {
			throw new YYPayException(RspCodeEnum.COMMON_ERROR_NO_SIGN_AUTHENTICATION, "签名失败", "无效请求");
		}

		return true;
	}

	@Override
	public boolean validate() throws Exception {
		return false;
	}

}
